package com.yunti.yntk.api;

import android.content.Context;
import android.content.SharedPreferences;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.nio.charset.Charset;

public class TokenInterceptor implements Interceptor {

    private static final Charset UTF8 = Charset.forName("UTF-8");

    private Context context;

    public TokenInterceptor(Context context) {
        this.context = context;
    }


    @Override
    public Response intercept(Chain chain) throws IOException {
        okhttp3.Request originalRequest = chain.request();
        SharedPreferences sp = context.getSharedPreferences("loginToken", 0);
        String token = sp.getString("token","");
        if (token.equals("")) {
            return chain.proceed(originalRequest);
        }

        Request authorised = originalRequest.newBuilder()
                .header("Authorization",  "Bearer "+token)
                .build();
        return chain.proceed(authorised);
    }



}